package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniqueEmployee;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.SysDimEmployee;
import cn.micro.system.manage.entity.dto.SysDimEmployeeSearch;
import cn.micro.system.manage.entity.dto.UpdateStatusDTO;
import cn.micro.system.manage.service.SysDimEmployeeService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/manage/sysDimEmployee")
@Validated
@RequiredArgsConstructor
public class SysDimEmployeeController {

    private final SysDimEmployeeService sysDimEmployeeService;

    @GetMapping("{page}/{size}")
    @PreAuthorize("hasAuthority('bnt.employee.list')")
    public Object list(@PathVariable int page, @PathVariable int size, SysDimEmployeeSearch search){
        return Result.success(new PageInfo<>(sysDimEmployeeService.listByPage(page, size, search)));
    }

    @GetMapping("/detail/{id}")
    @PreAuthorize("hasAuthority('bnt.employee.list')")
    public Object detail(@PathVariable Long id){
        return Result.success(sysDimEmployeeService.getById(id));
    }

    @PostMapping("save")
    @Log(title = "员工管理--新增员工信息表", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.employee.add')")
    public Object save(@Validated(value = ValidateGroup.add.class) @UniqueEmployee @RequestBody SysDimEmployee sysDimEmployee){
        if (!sysDimEmployeeService.saveEmployee(sysDimEmployee)){
            return Result.fail();
        }
        return Result.success();
    }

    @PutMapping("edit")
    @Log(title = "员工管理--编辑员工信息表", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.employee.update')")
    public Object edit(@Validated(value = ValidateGroup.edit.class) @UniqueEmployee @RequestBody SysDimEmployee sysDimEmployee){
        if (!sysDimEmployeeService.updateById(sysDimEmployee)){
            return Result.fail();
        }
        return Result.success();
    }


    @GetMapping("remove/{id}")
    @Log(title = "员工管理--删除员工信息表", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.employee.remove')")
    public Object remove(@PathVariable Long id){
        sysDimEmployeeService.removeEmployee(id);
        return Result.success();
    }

    @PostMapping("updateStatus")
    @Log(title = "员工管理--更新员工信息状态", businessType = BusinessType.UPDATE)
    public Object updateStatus(@RequestBody UpdateStatusDTO dto){
        if (!sysDimEmployeeService.updateStatus(dto)){
            return Result.fail();
        }
        return Result.success();
    }

}
